package my.mall.admin.api.controller

import jakarta.validation.Valid
import jakarta.validation.constraints.NotEmpty
import jakarta.validation.constraints.NotNull
import my.mall.admin.api.pojo.dict.DictReq
import my.mall.admin.api.service.DictionaryService
import my.mall.admin.api.pojo.dict.DictResp
import my.mall.admin.api.pojo.dict.DictTreeResp
import my.mall.common.pojo.CommonResp
import my.mall.common.pojo.PageParam
import my.mall.common.pojo.PageResp
import my.mall.common.util.RespUtils
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("dict")
class DictionaryController(
    private val dictionaryService: DictionaryService
) {

    @GetMapping("name/list")
    fun getDictList(@RequestParam @Valid @NotEmpty nameList: List<String>): CommonResp<List<DictTreeResp>> {
        return dictionaryService.getDictList(nameList)
    }

    @GetMapping("parent/list")
    fun getParentList(name: String?, pageParam: PageParam): CommonResp<PageResp<DictResp>> {
        return dictionaryService.getParentDictList(name, pageParam)
    }

    @GetMapping("children/list")
    fun getListByPid(@RequestParam @Valid @NotNull pId: Int): CommonResp<List<DictResp>> {
        return dictionaryService.getListByPid(pId)
    }

    @PostMapping("delete")
    fun deleteDict(@RequestParam @Valid @NotNull id: Int): CommonResp<*> {
        dictionaryService.deleteDict(id)
        return RespUtils.success()
    }

    @PostMapping("save")
    fun saveDict(@RequestBody @Valid dict: DictReq): CommonResp<*> {
        dictionaryService.saveDict(dict)
        return RespUtils.success()
    }
}
